package com.sg.service.base.component.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.component.req.BatchQueryChannelReq;
import com.sg.dto.base.component.req.QueryChannelDetailReq;
import com.sg.entity.OmsMessageChannel;
import com.sg.service.base.component.MOmsMessageChannelService;
import com.wicket.okrcomponent.common.dto.FieldInfo;
import com.wicket.okrcomponent.common.exception.BizException;
import com.wicket.okrcomponent.common.tools.StringUtils;
import com.wicket.okrcomponent.dal.mapper.mbg.OmsMessageChannelMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsMessageChannelServiceImpl
        extends ServiceImpl<OmsMessageChannelMapper, OmsMessageChannel>
        implements MOmsMessageChannelService {

    @Autowired
    OmsMessageChannelMapper omsMessageChannelMapper;

    /**
     * 2-5-08查询通道详情[6808]
     * gen by moon at 5/17/2023, 3:41:20 AM
     */
    @Trace(operationName = "2-5-08查询通道详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsMessageChannel queryChannelDetail(QueryChannelDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<OmsMessageChannel> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsMessageChannel> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsMessageChannel retData = retList.get(0);
                retData.setChannelLogo(CommonFunctionHelper.getFilePath(retData.getChannelLogo()));
                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-5-08新增通道[6809]
     * gen by moon at 8/15/2023, 8:32:04 PM
     */
    @Trace(operationName = "2-5-08新增通道")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addChannel(OmsMessageChannel omsMessageChannel) {
        // TODO: 2024/1/19
        if (!StringUtils.isEmpty(omsMessageChannel.getChannelLogo())) {
            omsMessageChannel.setChannelLogo(CommonFunctionHelper.replaceFilePath(omsMessageChannel.getChannelLogo()));
        }
        omsMessageChannel.setMessageChannelId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsMessageChannel.setIsValid("FALSE");
        if (super.save(omsMessageChannel)) {
            return omsMessageChannel.getMessageChannelId();
        } else {
            return "-1";
        }

    }

    /**
     * 2-5-08修改通道[6811]
     * gen by moon at 5/17/2023, 3:41:21 AM
     */
    @Trace(operationName = "2-5-08修改通道")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateChannel(OmsMessageChannel omsMessageChannel) {

        // TODO: 2024/1/19
        if (!StringUtils.isEmpty(omsMessageChannel.getChannelLogo())) {
            omsMessageChannel.setChannelLogo(CommonFunctionHelper.replaceFilePath(omsMessageChannel.getChannelLogo()));
        }
        return super.updateById(omsMessageChannel);
    }

    /**
     * 2-5-08批量查询通道[7012]
     * gen by moon at 1/22/2024, 11:04:35 PM
     */
    @Trace(operationName = "2-5-08批量查询通道")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsMessageChannel> batchQueryChannel(BatchQueryChannelReq reqDto) {

        try {
            QueryWrapper<OmsMessageChannel> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getChannelList() == null || reqDto.getChannelList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("message_channel_id", reqDto.getChannelList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsMessageChannel.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsMessageChannel.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsMessageChannel.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            // TODO: 2024/5/30  
            List<OmsMessageChannel> retList = super.list(queryWrapper);
            for (OmsMessageChannel one : retList) {
                one.setChannelLogo(CommonFunctionHelper.getFilePath(one.getChannelLogo()));
            }
            return retList;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-5-08删除通道[7072]
     * gen by moon at 6/1/2023, 2:15:41 AM
     */
    @Trace(operationName = "2-5-08删除通道")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteChannel(String messageChannelId) {

        return super.removeById(messageChannelId);
    }
}
